package org.dromara.pe.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.pe.domain.bo.CertificateBo;
import org.dromara.pe.domain.vo.CertificateVo;
import org.dromara.pe.domain.Certificate;
import org.dromara.pe.mapper.CertificateMapper;
import org.dromara.pe.service.ICertificateService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 体测合格证书Service业务层处理
 *
 * @author Lion Li
 * @date 2024-09-02
 */
@RequiredArgsConstructor
@Service
public class CertificateServiceImpl implements ICertificateService {

    private final CertificateMapper baseMapper;

    /**
     * 查询体测合格证书
     */
    @Override
    public CertificateVo queryById(Long certificateId){
        return baseMapper.selectVoById(certificateId);
    }

    /**
     * 查询体测合格证书列表
     */
    @Override
    public TableDataInfo<CertificateVo> queryPageList(CertificateBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Certificate> lqw = buildQueryWrapper(bo);
        Page<CertificateVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询体测合格证书列表
     */
    @Override
    public List<CertificateVo> queryList(CertificateBo bo) {
        LambdaQueryWrapper<Certificate> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Certificate> buildQueryWrapper(CertificateBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Certificate> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getStuNum()), Certificate::getStuNum, bo.getStuNum());
        lqw.eq(StringUtils.isNotBlank(bo.getScore1()), Certificate::getScore1, bo.getScore1());
        lqw.eq(StringUtils.isNotBlank(bo.getScore2()), Certificate::getScore2, bo.getScore2());
        lqw.eq(StringUtils.isNotBlank(bo.getScore3()), Certificate::getScore3, bo.getScore3());
        lqw.eq(StringUtils.isNotBlank(bo.getScore4()), Certificate::getScore4, bo.getScore4());
        lqw.eq(StringUtils.isNotBlank(bo.getGraduationScore()), Certificate::getGraduationScore, bo.getGraduationScore());
        return lqw;
    }

    /**
     * 新增体测合格证书
     */
    @Override
    public Boolean insertByBo(CertificateBo bo) {
        Certificate add = MapstructUtils.convert(bo, Certificate.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setCertificateId(add.getCertificateId());
        }
        return flag;
    }

    /**
     * 修改体测合格证书
     */
    @Override
    public Boolean updateByBo(CertificateBo bo) {
        Certificate update = MapstructUtils.convert(bo, Certificate.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Certificate entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除体测合格证书
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
